package com.wclass.mapper;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wclass.entity.BaseAppUser;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户 Mapper 接口
 * </p>
 *
 * @author 森森
 * @since 2018-08-28
 */
public interface BaseAppUserMapper extends BaseMapper<BaseAppUser> {


    /**
     * 扣除提现金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update base_app_user set withdraw_amount=withdraw_amount-#{amount} where id =#{userId} and " +
            "withdraw_amount>=${amount}")
    int deductionsWithdrawAmount(@Param("userId") Long userId, @Param("amount") Long amount);


    /**
     * 增加提现金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update base_app_user set withdraw_amount=withdraw_amount+#{amount} where id =#{userId}")
    int addWithdrawAmount(@Param("userId") Long userId, @Param("amount") Long amount);


    /**
     * 扣除可提现
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update base_app_user set amount=amount-#{amount} where id =#{userId} and amount>=#{amount}")
    int deductionsAmount(@Param("userId") Long userId, @Param("amount") Long amount);


    /**
     * 增加可提现
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update base_app_user set amount=amount+#{amount} where id =#{userId}")
    int addAmount(@Param("userId") Long userId, @Param("amount") Long amount);


    /**
     * 扣除冻结金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update base_app_user set thawing_amount=thawing_amount-#{amount} where id =#{userId} and " +
            "thawing_amount>=#{amount}")
    int deductionsThawingAmount(@Param("userId") Long userId, @Param("amount") Long amount);


    /**
     * 增加冻结金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update base_app_user set thawing_amount=thawing_amount+#{amount} where id =#{userId}")
    int addThawingAmount(@Param("userId") Long userId, @Param("amount") Long amount);

    /**
     * <p>
     * 根据 entity 条件，查询全部记录（并翻页）
     * </p>
     *
     * @param rowBounds 分页查询条件（可以为 RowBounds.DEFAULT）
     * @param wrapper   实体对象封装操作类（可以为 null）
     * @return List<T>
     */
    @Override
    @Select("select *,(amount+thawing_amount+withdraw_amount) as all_amount ${ew.sqlSelect} from base_app_user where " +
            "state=0 ${ew.sqlSegment}")
    List<BaseAppUser> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<BaseAppUser> wrapper);


    /**
     * 更新排行
     *
     * @return
     */
    @Update("update base_app_user target,(SELECT ( @rowNO := @rowNo + 1 ) AS rowno, a.* FROM ( SELECT *, ( amount + " +
            "thawing_amount + withdraw_amount ) AS all_amount FROM base_app_user WHERE state = 0  ) a, ( SELECT " +
            "@rowNO := 0 ) b ) s  set target.ranking_num=s.rowno where target.id=s.id")
    int upRankingNum();

    /**
     * 通过机构id查询该机构下所有的老师
     * @param id
     * @return
     */
    @Select("select * from base_app_user  where institution_id=#{id}")
    List<BaseAppUser> selectByInstitutionId(Integer id);

    /**
     * 通过状态查询所有的老师
     * @param auditStatus
     * @return
     */
    @Select("select * from base_app_user  where audit_status=#{auditStatus} and state=0")
    List<BaseAppUser> getListByAuditStatus(@Param("auditStatus") int auditStatus );

    /**
     * 禁用或者恢复账号
     *
     * @param userId
     * @param disabled
     * @return
     */
    @Update("update base_app_user set disabled=#{disabled} where id =#{userId}")
    int updateDisabled(@Param("userId") Long userId, @Param("disabled") Integer disabled);

    /**
     * 取消用户机构的绑定状态
     * @param id
     * @return
     */
    @Update("update base_app_user set type = 0,rold_id = null,institution_id = null,institution_invite_state = null where id =#{userId}")
    int updateUserOrganBind(@Param("userId") Long id);

    /**
     * 更新用户学生数量
     * @param id
     * @return
     */
    @Update("update base_app_user set student_num = (select count( distinct co.pay_person_id) from coures_order co where co.user_id = #{userId}) where id =#{userId}")
    int updateStudentNum(@Param("userId") Long id);

}
